<template>
	<view class="list-view">
		<view class="top-bar"><uni-navbar :show="show" @click-show="show = !show" :fixed="true" :back="true" color="#fff" :title="title"></uni-navbar></view>

		<view class="filter-box" v-if="show">
			<view class="form-box">
				<u-form ref="uForm">
					<u-form-item class="form-item" label="标题"><u-input v-model="hyhdMeeting.title" type="text" placeholder="请输入标题" /></u-form-item>
					<u-form-item class="form-item" label="类别">
						<u-input v-model="meetingType" type="select" :select-open="meetingTypeShow" placeholder="请选择类别" @click="meetingTypeShow = true" />
						<u-select :list="meetingTypeList" v-model="meetingTypeShow" @confirm="typeConfirm"></u-select>
					</u-form-item>
					<u-form-item class="form-item" label="开始时间">
						<date-time-picker placeholder="请选择开始时间" :defaultValue="startTime" fields="minute" @change="timeChange($event, 'startTime')"></date-time-picker>
					</u-form-item>
					<u-form-item class="form-item" label="结束时间">
						<date-time-picker placeholder="请选择结束时间" :defaultValue="endTime" fields="minute" @change="timeChange($event, 'endTime')"></date-time-picker>
					</u-form-item>
				</u-form>
			</view>
			<view class="fiexd-btm">
				<view class="flex-box">
					<u-button class="form-btn" type="default" @click="resert">清除</u-button>
					<u-button class="form-btn" type="error" @click="filter">确定</u-button>
				</view>
			</view>
		</view>

		<view class="search-view">
			<u-search class="search-input" shape="square" bg-color="#fff" v-model="hyhdMeeting.title" :show-filter="true" @search="search" @custom="show = !show"></u-search>
		</view>
		<u-tabs class="uTabs" :list="tabsList" :current="tabsCurrent" @change="tabsChange"></u-tabs>

		<view class="list-box" :style="{ background: loading == 'nodata' ? 'none' : '#fff' }">
			<view class="list-info_01" v-for="(item, index) of meetingList" :key="item.id" @click="navTo(item)">
				<view class="info-title">{{ item.title }}</view>
				<view class="info-btm">
					<view class="btm-left clamp">
						<text class="left-name" v-if="types == 'jiebie'">{{ item.jiebieCode }}</text>
						<text class="left-name" v-else>{{ item.meetingType }}</text>
						<text class="left-time">{{ item.startTime | parseTime('{y}-{m}-{d}') }}</text>
					</view>
					<view class="btm-right">
						<view class="right-type" :style="'color:' + colorMap[item.meetingState]">{{ item.meetingState }}</view>
					</view>
				</view>
			</view>
			<u-loadmore :status="loading" />
		</view>

		<u-back-top :scroll-top="scrollTop" top="200"></u-back-top>
	</view>
</template>

<script>
import dateTimePicker from '@/components/uni-datetime-picker';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker
	},
	data() {
		return {
			scrollTop: 0,
			show: false,
			tabsCurrent: 0,
			tabsList: [
				{
					name: '所有',
					value: 0
				},
				{
					name: '报名中',
					value: 1
				},
				{
					name: '报名截止',
					value: 2
				},
				{
					name: '进行中',
					value: 3
				},
				{
					name: '已结束',
					value: 4
				}
			],
			colorMap: {
				报名中: '#303133',
				报名截止: '#0099ff',
				进行中: '#ff9900',
				结束: '#19be6b'
			},
			nodeStatus: 0,
			meetingState: {
				0: '',
				1: 'reg',
				2: 'reg_end',
				3: 'going',
				4: 'end'
			},
			title: '会议列表',
			types: 'huiyi',

			meetingType: '',
			meetingTypeShow: false,
			meetingTypeList: [],

			startTime: '',
			endTime: '',
			meetingList: [],

			hyhdMeeting: {
				clientCode: '',
				meetingType: '',
				meetingState: '',
				remark1: 'huiyi',
				title: ''
			},
			loading: 'loadmore'
		};
	},
	onLoad(option) {
		this.loadLogin();
		if (option && option.type) {
			this.types = option.type;
			this.hyhdMeeting.remark1 = option.type;
			if (option.type == 'huodong') {
				this.title = '活动列表';
			}
			if (option.type == 'jiebie') {
				this.title = '界别活动';
			}
		}

		uni.setNavigationBarTitle({
			title: this.title
		});
		if (this.types == 'huiyi') {
			this.loadDict('hylx', 'meetingTypeList');
		} else {
			this.loadDict('hdlx', 'meetingTypeList');
		}
		if (option && option.meetingType && option.meetingType != '无') {
			this.meetingType = option.meetingType;
		}
	},
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	onReachBottom() {
		if (this.loading == 'loadmore') this.loadData('add');
	},
	onPageScroll(res) {
		this.scrollTop = res.scrollTop;
	},
	computed: {
		dateRangeSea: {
			get: function () {
				let time = this.startTime + ' - ' + this.endTime;
				if (this.startTime == '' || this.endTime == '') {
					return '';
				} else {
					return time;
				}
			},
			set: function (e) {
				this.startTime = '';
				this.endTime = '';
			}
		},
		...mapState(['userInfo'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.Authorization) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;
				this.hyhdMeeting.clientCode = this.userInfo.sysUser.clientCode;
				this.hyhdMeeting.userId = this.userInfo.sysUser.id;

				this.pageNum = 1;
				this.pageSize = 10;
			} else {
				this.$logout();
			}
		},
		loadDict(type, list) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type
				}
			}).then((res) => {
				if (!res.data) {
					return false;
				}
				this[list] = [];
				res.data.forEach((item) => {
					this[list].push({
						value: item.dictValue,
						label: item.dictName
					});
					if (this.meetingType == item.dictName) {
						this.hyhdMeeting.meetingType = item.dictValue;
					}
				});
				this.loadData();
			});
		},
		loadData(type = 'add') {
			if (this.loading == 'loading') {
				return false;
			}
			if (type == 'add') {
				if (this.loading == 'nomore') {
					return false;
				}
				this.loading = 'loading';
			} else {
				this.loading = 'loading';
			}
			if (type != 'add') {
				this.pageNum = 1;
			}

			let data = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				hyhdMeeting: this.hyhdMeeting,
				clientCode: this.hyhdMeeting.clientCode,
				meetingState: this.hyhdMeeting.meetingState,
				startTime: this.dateRangeSea,
				meetingType: this.hyhdMeeting.meetingType,
				remark1: this.hyhdMeeting.remark1,
				title: this.hyhdMeeting.title,
				curUserId: this.hyhdMeeting.userId,
				userId: this.hyhdMeeting.userId
			};
			let url = '/huiyihuodong/hyhd-meeting-user/listMyMeetings';
			if (this.$sysMenus('hy_list') || this.$sysMenus('hd_list')) {
				url = '/huiyihuodong/hyhd-meeting/list';
			}

			this.$request(url, data, {
				load: true
			}).then((res) => {
				let count = 0;
				if (type != 'add') {
					uni.stopPullDownRefresh();
					this.meetingList = [];
				}
				this.loading = this.pageNum < Math.ceil(res.data.total / this.pageSize) ? 'loadmore' : 'nomore';
				this.pageNum = this.pageNum + 1;
				this.meetingList = this.meetingList.concat(res.data.records);
				this.loaded = true;
				if (this.meetingList.length <= 0) {
					this.loading = 'nodata';
				}
			});
		},
		timeChange({ date, dt }, idx) {
			this[idx] = date.year + '-' + date.month2 + '-' + date.day2 + ' ' + date.hour2 + ':' + date.minute2 + ':' + '00';
		},
		tabsChange(index) {
			this.tabsCurrent = index;
			this.type(this.tabsList[index].value);
		},
		typeConfirm(e) {
			this.meetingType = e[0].label;
			this.hyhdMeeting.meetingType = e[0].value;
		},
		filter() {
			if (this.startTime == '' && this.endTime != '') {
				this.$msg('请选择开始时间');
				return false;
			}
			if (this.startTime != '' && this.endTime == '') {
				this.$msg('请选择结束时间');
				return false;
			}
			let arr = this.startTime.split('-');
			let startTime = new Date(arr[0], arr[1], arr[2]).getTime();

			let arrs = this.endTime.split('-');
			let endTime = new Date(arrs[0], arrs[1], arrs[2]).getTime();

			if (startTime >= endTime) {
				this.$msg('开始时间应小于结束时间');
				return false;
			}

			this.show = false;
			this.loadData('refresh');
		},
		resert() {
			this.show = false;
			this.hyhdMeeting.title = '';
			this.meetingType = '';
			this.hyhdMeeting.meetingType = '';
			this.dateRangeSea = '';
			this.loadData('refresh');
		},
		search(e) {
			this.hyhdMeeting.title = e;
			this.loadData('refresh');
		},
		type(e) {
			this.nodeStatus = e;
			this.hyhdMeeting.meetingState = this.meetingState[e];
			this.loadData('refresh');
		},
		navTo(item) {
			uni.navigateTo({
				url: '/pagesB/meeting/detail?id=' + item.id
			});
		}
	}
};
</script>

<style lang="scss">
.list-box {
	padding: 0 30rpx;
	background-color: #fff;
}

.filter-box {
	position: fixed;
	left: 0;
	top: 0;
	bottom: 0;
	right: 0;
	z-index: 998;
	background-color: #fff;
}

.form-box {
	.form-item {
		@include mp-flex;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
